<?php
namespace app\admin\logic;
use app\admin\code\AuthCode;
use app\admin\code\BaseCode;
use app\admin\controller\Base;
use app\admin\model\AuthModel;
use think\Cache;
use think\Env;
use think\exception\HttpException;
use think\Lang;
use think\Request;

class AuthLogic extends BaseLogic
{
    protected $_no_auth_auth = [
        'admin/login/index',
        'admin/login/login',
        'admin/admin/logout',
        'admin/login/index',
        'admin/login/login',
        'admin/index/index',
        'admin/admin/edit_password',
    ];//所有人可以执行的节点

    /**
     * @param string $module 模块
     * @param string $controller 控制器
     * @param string $action 动作
     * @param callable $authCallBack  权限不足时回调
     * @return bool
     */
    public function auth($module = '',$controller = '',$action = '',$authCallBack = null){
        $node = $module.'/'.$controller.'/'.$action;
        $admin_info = Base::get_admin_info();
        if(in_array($node,$this->_no_auth_auth) || $admin_info['is_super'] == 1){//该节点不需要任何权限
            return true;
        }else{//判断权限
            $auth_array = $this->_get_all_auth();
            if(in_array($node,$auth_array)){
                $auth_id = array_search($node,$auth_array);
                if(in_array($auth_id,$admin_info['auth'])){
                    return true;
                }else{
                    if($authCallBack){
                        $authCallBack();
                    }else{
                        return false;
                    }
                }
            }else{
                throw new HttpException('404','权限不存在');
            }
        }
        return true;
    }

    /**
     * 获取当前网站所有的权限
     */
    private function _get_all_auth(){
        $auth = Cache::get('ALL_AUTH');
        $auth = false;
        if($auth == false){
            $model = new AuthModel();
            $data = $model->get_all_auth();
            $auth = [];
            foreach ($data as $value){
                if($value['module'] != '' && $value['controller'] != '' && $value['action'] != ''){
                    $auth[$value['id']] = $value['module'].'/'.$value['controller'].'/'.$value['action'];
                }
            }
            Cache::set('ALL_AUTH',$auth);
        }
        return $auth;
    }
    protected function _after_del_validate(){
        $model = new AuthModel();
        if($model->deleteTrue(['id'=>$this->_param['id']])){
            $this->result = true;
            return true;
        }else{
            $this->err = ['code'=>BaseCode::FAIL,'msg'=>Lang::get(BaseCode::FAIL)];
            $this->result = false;
            return false;
        }
    }
    protected function _after_update_validate(){
        $model = new AuthModel();
        $where = [];
        $where['id'] = $this->_param['id'];
        unset($this->_param['id']);
        if($model->updateData($where,$this->_param)){
            $this->result = true;
            return true;
        }else{
            $this->err = ['code'=>BaseCode::FAIL,'msg'=>Lang::get(BaseCode::FAIL)];
            $this->result = false;
            return false;
        }
    }
    protected function _after_add_validate()
    {
        $model = new AuthModel();
        return $model->addData($this->_param);
    }
}


